$(document).ready(function(){
    var tlfyMaxScore = 248.5;
    $("#createImgBtn").on("click",function(){
        var youName = $("#youNameInput").val();
        var schoole = $("#schooleInput").val();
        var cetType = $("#cetTypeInput").find("option:selected").html();
        var totalScore = $("#totalScroreInput").val();
        
        if(isNull(youName)){
            alert("请填写姓名");
            return;
        }
        if(isNull(schoole)){
            alert("请填写学校");
            return;
        }
        if(isNull(totalScore)){
            alert("请填写你想要的分数");
            return;
        }
        
        if(isNaN(totalScore)){
            alert("总分必须为425到710之间数字");
            return;
        }
        
        if(totalScore>710 || totalScore<425){
            alert("总分必须为425到710之间的数字");
            return;
        }
        
        var tlScore = parseInt(totalScore/710*tlfyMaxScore)-parseInt(Math.random()*10);
        var ydScore = parseInt(totalScore/710*tlfyMaxScore)-parseInt(Math.random()*10);
        var xzScore =  totalScore-tlScore-ydScore;
        
        if(totalScore==710){
            tlScore = 248.5;
            ydScore = 248.5;
            xzScore = 213;
        }
        
        $("#youName").html(youName);
        $("#schoole").html(schoole);
        $("#cetType").html(cetType);
        $("#totalScore").html(totalScore);
        $("#tlScore").html(tlScore);
        $("#ydScore").html(ydScore);
        $("#xzScore").html(xzScore);
        
        $("div.collectInfo").hide();
        $("div.cetPanel").show();
        html2canvas( document.body ,{  		
            onrendered: function(canvas){
                //alert( canvas.toDataURL());
               /* $('#downloadBtn').show().attr( 'href' , canvas.toDataURL("image/png") ) ;
                $('#downloadBtn').attr( 'download' , '我的'+cetType+'成绩单.png' ) ;
                $("div.cetPanel").hide();*/
                $("body").append("<h3 id='tempTitle' style='width:100%;text-align:center'>＞请长按保存图片或截图然后发朋友圈吧＜</h3>");
                $("div.cetPanel").hide();
               /* var downloadBtn = $('<br/><a href="'+canvas.toDataURL("image/png")+'" download="我的'+cetType+'成绩单.png" style="font-size:1.5rem">下载成绩单</a>');
                $("body").append(downloadBtn);*/
                var image = new Image();
                image.src = canvas.toDataURL("image/png");
                image.width = $(document).width();
                document.body.appendChild(image);
                setTimeout(function(){
                    $("#tempTitle").hide();
                    $("body,html").css("background","none");
                },3000);
            },
            background:"#ffffff",
            height:511,
            width:640
        });
    });
});

function isNull(val){
    if(!val || val=="null" || val=="undefined"){
        return true;
    }
    return false;
}

document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
  WeixinJSBridge.call('hideOptionMenu');
}, false);